package com.xixi.core.rpc.protocol;

import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zxh
 * @date: 2024/8/26 10:59
 * @description: 协议消息的序列化枚举
 */
@Getter
public enum ProtocolMessageSerializerEnum {

	JDK("jdk", 0),
	JSON("json", 1),
	KRYO("kryo", 2),
	HESSIAN("hessian", 3);

	private final String value;

	private final int key;

	ProtocolMessageSerializerEnum(String value, int key) {
		this.key = key;
		this.value = value;
	}

	/**
	 * 获取值列表
	 * @return 返回值列表
	 */
	public static List<String> getValues() {
		return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
	}

	/**
	 * 根据可以获取枚举值
	 * @param key 序列化器对应的key
	 * @return 返回对应的序列化器枚举值
	 */
	public static ProtocolMessageSerializerEnum getEnumByKey(int key) {
		for (ProtocolMessageSerializerEnum anEnum : ProtocolMessageSerializerEnum.values()) {
			if (anEnum.key == key) {
				return anEnum;
			}
		}
		return null;
	}

	/**
	 * 根据value获取枚举值
	 * @param value 序列化器对应的值
	 * @return 返回对应的枚举值
	 */
	public static ProtocolMessageSerializerEnum getEnumByValue(String value) {
		if (ObjectUtil.isEmpty(value)) {
			return null;
		}
		for (ProtocolMessageSerializerEnum anEnum : ProtocolMessageSerializerEnum.values()) {
			if (anEnum.value.equals(value)) {
				return anEnum;
			}
		}
		return null;
	}
}
